#!/bin/sh

set -e

. ./conf.sh
. ./util.sh


################################################################################
#-------------------------------------------------------------------------------
gnu_target=x86_64-nyan-linux-gnu
linux_arch_target=x86_64
#-------------------------------------------------------------------------------
cross_dir_root=/home/nyan-build/cross/$gnu_target
sysroot=$cross_dir_root/sysroot
target_linux_version=3.16.3
################################################################################


umask 022


#===============================================================================
#we want the bootstrap glibc to be able to run on the bootstrap kernel since we
#will run in a chroot for the second phase of installation... *AND* target
#linux
bootstrap_linux_version=$(uname -r \
                           | egrep -o '[[:digit:]]\.[[:digit:]]+\.[[:digit:]]+')
#===============================================================================

#we need to tell our glibc which version of the linux kernel we need it to run
#onto
oldest_linux_version_get $bootstrap_linux_version $target_linux_version


#===============================================================================
#linux kernel related
. "$builders_dir_root/linux-src/builder-cross.sh"
#here we apply/"build" some modifications/additions to the linux kernel source
. "$builders_dir_root/linux-gpu-amd-si/builder-cross.sh"
. "$builders_dir_root/linux-headers/builder-cross.sh"
. "$builders_dir_root/linux-firmware/builder-cross.sh"
#===============================================================================


################################################################################
#time to enable the cross toolchain
export "PATH=$cross_dir_root/bin:$PATH"
export "LD_LIBRARY_PATH=$cross_dir_root/lib"
################################################################################


#==============================================================================
. "$builders_dir_root/glibc/builder-cross.sh"
#after dash installation, you can chroot and perform basic checks
. "$builders_dir_root/dash/builder-cross.sh"
. "$builders_dir_root/gmp/builder-cross.sh"
. "$builders_dir_root/mpfr/builder-cross.sh"
. "$builders_dir_root/mpc/builder-cross.sh"
. "$builders_dir_root/binutils/builder-cross.sh"
. "$builders_dir_root/gcc/builder-cross.sh"
. "$builders_dir_root/sed/builder-cross.sh"
. "$builders_dir_root/coreutils/builder-cross.sh"
. "$builders_dir_root/grep/builder-cross.sh"
. "$builders_dir_root/gawk/builder-cross.sh"
. "$builders_dir_root/make/builder-cross.sh"
. "$builders_dir_root/zlib/builder-cross.sh"
. "$builders_dir_root/gzip/builder-cross.sh"
. "$builders_dir_root/bzip2/builder-cross.sh"
. "$builders_dir_root/xz/builder-cross.sh"
. "$builders_dir_root/tar/builder-cross.sh"
#===============================================================================


################################################################################
mkdir -p "$sysroot/dev"
mkdir -p "$sysroot/proc"
mkdir -p "$sysroot/sys"
mkdir -p "$sysroot/etc"
mkdir -p "$sysroot/root"
mkdir -p "$sysroot/tmp"
chmod 1777 "$sysroot/tmp"
################################################################################


################################################################################
#create a passwd file with the root account. Git *wants* it (bitch!).
echo 'root::0:0::/root:/bin/sh' >"$sysroot/etc/passwd"
chmod 644 "$sysroot/etc/passwd"
################################################################################
